package com.xcw.bos.web.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 流程实例管理Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class ProcessInstanceAction extends ActionSupport {
    @Resource
    private ProcessEngine processEngine;
    
    // 查询正在运行的流程实例数据-----查询表act_ru_execution
    public String list(){
    	// 流程实例查询对象
    	ProcessInstanceQuery query = processEngine.getRuntimeService()
    			.createProcessInstanceQuery();
    	// 添加排序---按照id
    	query.orderByProcessInstanceId().desc();
    	List<ProcessInstance> list = query.list();
    	
    	ActionContext.getContext().getValueStack().set("list", list);
    	return "list";
    }
    
    private String id;//流程实例id
    // 获取流程变量
    public String findData() throws IOException{
    	Map<String, Object> map = processEngine.getRuntimeService()
    			.getVariables(id);
    	ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
    	ServletActionContext.getResponse().getWriter().print(map);
    	return NONE;
    }
    
    public void setId(String id){
    	this.id = id;
    }
    
    // 根据流程实例id查询deploymentId pngName x y width height
    public String showPng(){
    	// 1、根据流程实例id查询流程实例对象
    	ProcessInstance processInstance = processEngine.getRuntimeService()
    			.createProcessInstanceQuery().processInstanceId(id).singleResult();
    	
    	// 2、根据流程实例对象获取流程定义id
    	String processDefinitionId = processInstance.getProcessDefinitionId();
    	
    	// 3、根据流程定义id查询流程定义对象
    	ProcessDefinition processDefinition = processEngine.getRepositoryService()
    			.createProcessDefinitionQuery().processDefinitionId(processDefinitionId)
    			.singleResult();
    	
    	//4、 根据流程定义对象获取部署id
    	deploymentId = processDefinition.getDeploymentId();
    	
    	//5、根据流程定义对象获取png图片名称
    	resourceName = processDefinition.getDiagramResourceName();
    	
		//6、获取当前流程实例运行到那个任务节点
        String activityId = processInstance.getActivityId();//usertask1
        
        //7、根据流程定义id获取一个包含有坐标信息的流程定义对象
        ProcessDefinitionEntity pd = (ProcessDefinitionEntity) processEngine
        		.getRepositoryService().getProcessDefinition(processDefinitionId);
        
        ActivityImpl activityImpl = pd.findActivity(activityId);
        
        //8、获取当前任务节点的坐标数据
        int x = activityImpl.getX();
        int y = activityImpl.getY();
        int width = activityImpl.getWidth();
        int height = activityImpl.getHeight();
        
        ActionContext.getContext().getValueStack().set("deploymentId", deploymentId);
        ActionContext.getContext().getValueStack().set("imageName", resourceName);
		ActionContext.getContext().getValueStack().set("x", x-1);
		ActionContext.getContext().getValueStack().set("y", y-1);
		ActionContext.getContext().getValueStack().set("width", width);
		ActionContext.getContext().getValueStack().set("height", height);
		return "showPng";
    }
    
    String deploymentId;
    
    public void setDeploymentId(String deploymentId){
    	this.deploymentId = deploymentId;
    }
    
    public void setResourceName(String resourceName){
    	this.resourceName = resourceName;
    }
    
    String resourceName;
    
     // 获取png图片输入流程，文件下载
    public String viewImage(){
    	InputStream pngStream = processEngine.getRepositoryService()
    			.getResourceAsStream(deploymentId, resourceName);
    	ActionContext.getContext().put("pngStream", pngStream);
    	return "viewImage";
    }
    
}
